﻿<Upload Style="UploadStyle.Card" DefaultFileList="@DefaultFormatFileList" OnChange="@OnCardUpload" OnDelete="@(fileName => Task.FromResult(true))"></Upload>

@code {
    private List<UploadFile> DefaultFormatFileList { get; } = new List<UploadFile>()
    {
        new UploadFile { FileName = "Test.xls" },
        new UploadFile { FileName = "Test.doc" },
        new UploadFile { FileName = "Test.ppt" },
        new UploadFile { FileName = "Test.mp3" },
        new UploadFile { FileName = "Test.mp4" },
        new UploadFile { FileName = "Test.pdf" },
        new UploadFile { FileName = "Test.cs" },
        new UploadFile { FileName = "Test.zip" },
        new UploadFile { FileName = "Test.txt" },
        new UploadFile { FileName = "Test.dat" }
    };

    private CancellationTokenSource? ReadToken { get; set; }
    private async Task OnCardUpload(IEnumerable<UploadFile> files)
    {
        var file = files.FirstOrDefault();
        if (file != null && file.File != null)
        {
            // 服务器端验证当文件大于 2MB 时提示文件太大信息
            if (file.Size > 2 * 1024 * 1024)
            {
                await ToastService.Information("上传文件", $"文件大小超过 2MB");
                file.Code = 1;
                file.Error = "文件大小超过 2MB";
            }
            else
            {
                await SaveToFile(file);
            }
        }
    }
}
